/**
 * @Author: zy
 * @Company:
 * @Create Time: 2016�?8�?13�? 下午3:13:19
 */
package com.seckill.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


/**
 * md加密
 *
 * @author lill
 * @date 2019-06-25
 */
public class MdUtil {


    /**
     * 32位小写
     *
     * @param str
     * @return
     */
    public static String encodeLowerCase32(String str) {
        String reStr = null;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(str.getBytes());
            StringBuffer stringBuffer = new StringBuffer();
            for (byte b : bytes) {
                int bt = b & 0xff;
                if (bt < 16) {
                    stringBuffer.append(0);
                }
                stringBuffer.append(Integer.toHexString(bt));
            }
            reStr = stringBuffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return reStr;
    }

    /**
     * 32位大写
     *
     * @param str
     * @return String
     */
    public static String encodeUpperCase32(String str) {
        String reStr = encodeLowerCase32(str);
        if (reStr != null) {
            reStr = reStr.toUpperCase();
        }
        return reStr;
    }

    /**
     * 16位小写MD5
     *
     * @param str
     * @return String
     */
    public static String encodeLowerCase16(String str) {
        String reStr = encodeLowerCase32(str);
        if (reStr != null) {
            reStr = reStr.toUpperCase().substring(8, 24);
        }
        return reStr;
    }

    /**
     * 16位大写MD5
     *
     * @param str
     * @return String
     */
    public static String encodeUpperCase16(String str) {
        String reStr = encodeUpperCase32(str);
        if (reStr != null) {
            reStr = reStr.substring(8, 24);
        }
        return reStr;
    }


}